package com.pctx.bibadevicemanager.controller;

import com.github.pagehelper.PageHelper;
import com.pctx.bibacommon.result.Result;
import com.pctx.bibadevicemanager.pageHelper.PageInfo;
import com.pctx.bibadevicemanager.pojo.DeviceInfo;
import com.pctx.bibadevicemanager.service.DeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.temporal.Temporal;
import java.util.List;

//import com.github.pagehelper.PageInfo;

/**
 * Created by chason on 2018/2/28.18:57
 */
@RestController
@RequestMapping("device")
public class DeviceInfoController {
    @Autowired
    private DeviceInfoService deviceInfoService;

    @RequestMapping("list")
    public Result list(@RequestParam("companyUuid") String companyUuid) throws Exception {
        List<DeviceInfo> list = deviceInfoService.getByCompanyUuid(companyUuid);
        return Result.success(list);
    }

    @RequestMapping("page")
    public Result page(@RequestParam("companyUuid") String companyUuid,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        PageHelper.startPage(page, pageSize);
        List<DeviceInfo> list = deviceInfoService.getByCompanyUuid(companyUuid);
        return Result.success(new PageInfo<DeviceInfo>(list));
    }

    @RequestMapping("detail")
    public Result detail(@RequestParam("uuid") String uuid) throws Exception {
        DeviceInfo info = deviceInfoService.getByUuid(uuid);
        return Result.success(info);
    }

    @PostMapping("add")
    public Result add(DeviceInfo info) throws Exception {
        boolean flag = deviceInfoService.add(info);
        if (flag)
            return Result.success("添加成功");
        else
            return Result.error("添加失败");
    }

    /**
     * 前端扫描添加设备
     * @param imei
     * @param userUuid
     * @return
     * @throws Exception
     */
    @PostMapping("addByImei")
    public Result addByImei(@RequestParam(value = "imei", required = true) String imei,
                            @RequestParam(value = "companyUuid", required = true) String companyUuid,
                            @RequestParam(value = "deviceName", required = false) String deviceName,
                            @RequestParam(value = "userUuid", required = true) String userUuid) throws Exception {
        return deviceInfoService.addByImei(imei,userUuid,companyUuid,deviceName);
    }

    @PostMapping("edit")
    public Result edit(DeviceInfo info) throws Exception {
        boolean flag = deviceInfoService.updateByUuid(info);
        if (flag)
            return Result.success("修改成功");
        else
            return Result.error("修改失败");
    }

    @GetMapping("deviceList")
    public Result deviceListByUserUuid(@RequestParam(value = "userUuid", required = true) String userUuid,
                                       @RequestParam(value = "companyUuid", required = true) String companyUuid) throws Exception{
        return deviceInfoService.getListByUserUuid(userUuid,companyUuid);
    }
}
